/**
 * Created by MO on 2019/1/9.
 */
jQuery.noConflict(); // 让渡 $ 的使用权
(function ($) {

    /**
     * 设置审核,驳回状态
     */
    function setAuditStatus(status, pageNo) {
        $("#auditStatus").val(status);
        var $form = $("#crewListForm");
        $.post($form.attr("action"), $form.serialize(), function (data) {
            if (data == "true") {
                alert("保存成功!");
                if (status == 11 || status == 12 || status == 15) {
                    window.location.href = "/payroll/openList";
                }
            } else {
                alert("保存失败!");
                queryList($("#formIndex").get(0), pageNo);
            }
        });
    }

    window.setAuditStatus = setAuditStatus;

    /**
     * 发放薪酬
     */
    function addPayrollO() {
        var organizationId = $("#organizationId").val();
        if (organizationId == 0) {
            alert("请选择要发放薪酬的部门!");
        } else {
            $.post("/payroll/addPayroll/" + organizationId, function (data) {
                if (data == "true") {
                    alert("薪酬发放成功!");
                    queryList($("#formIndex").get(0), 1);
                } else {
                    alert("薪酬发放失败!");
                }
            });
        }
    }

    window.addPayrollO = addPayrollO;

    /**
     * 删除薪酬项目
     */
    function delPayrollO(obj, id, pageNo) {
        if (confirm("确认删除此组织机构工资单吗?")) {
            $.getJSON("/payroll/del/" + id, function (data) {
                if (data == true) {
                    alert("删除成功");
                    $(obj).parents("tr").remove();
                    queryList($("#formIndex").get(0), pageNo);
                } else {
                    alert("操作失败!请稍后再试....");
                }
            });
        }
    }

    window.delPayrollO = delPayrollO;

    /**
     * 计算薪酬发放
     */
    function crewTotalMoney(obj) {
        var totalMoney = 0;
        var $tr = $(obj).parents("tr");
        if (obj != null) {
            var regNum = /^\d+(\.\d+)?$/;
            if (!regNum.test($(obj).val())) {
                alert("请输入正确的正数数值!");
                $(obj).val(0);
                $(obj).focus();
            } else {
                var salList = $tr.find("td[name='proMoney_td'] input[type='number']");
                salList.each(function () {
                    totalMoney += parseFloat($(this).val());
                });
            }
        }
        var oldMoney = $tr.find("td[name='crewTotalMoney'] input").val();
        $tr.find("td[name='crewTotalMoney'] input").val(totalMoney);
        poTotalMoney(obj, totalMoney, oldMoney);
    }

    window.crewTotalMoney = crewTotalMoney;

    function dockPayBlur(obj) {
        var regNum = /^((-\d+(\.\d+)?)|(0+(\.0+)?))$/;
        if (!regNum.test($(obj).val())) {
            alert("请输入正确的负数数值!");
            $(obj).val(0);
            $(obj).focus();
            crewTotalMoney(obj);
        }
        var $crewTotalMoney = $(obj).parents("tr").find("td[name='crewTotalMoney'] input");
        var totalMoney = parseFloat($(obj).val()) + parseFloat($crewTotalMoney.val());
        var oldMoney = $crewTotalMoney.val();
        $crewTotalMoney.val(totalMoney);
        poTotalMoney(obj, totalMoney, oldMoney);
    }

    window.dockPayBlur = dockPayBlur;

    function poTotalMoney(obj, totalMoney, oldMoney) {
        var bad = parseFloat(totalMoney) - parseFloat(oldMoney);
        var poTotalMoney = $("#poTotalMoney").text();
        $("#poTotalMoney").text(parseFloat(poTotalMoney) + parseFloat(bad));
        $("#crewPoTotalMoney").val(parseFloat(poTotalMoney) + parseFloat(bad));
    }

    function setPoTotalMoney() {
        $("#crewListForm")[0].reset();
        $("#poTotalMoney").text($("#crewPoTotalMoney").val());
    }

    window.setPoTotalMoney = setPoTotalMoney;

    /**
     * 首次异步加载列表
     */
    //$("#operatorClass").removeClass("nav-item").addClass("nav-item active");
    queryList($("#formIndex").get(0), 1);
})(jQuery);